package com.douban.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.douban.app.mapper.LikeMapper;
import com.douban.app.mapper.UserMapper;
import com.douban.app.model.Dbuser;
import com.douban.app.model.Ilike;
import com.douban.app.service.UserService;
import com.github.yulichang.query.MPJQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LikeMapper likeMapper;

    @Override
    public Dbuser register(String num, String pass) {
        //判断num为空的情况
        if (!StringUtils.hasText(num)){
            return null;
        }
        //查询是否存在，若存在返回空
        QueryWrapper<Dbuser> wrapper = new QueryWrapper<>();
        wrapper.eq("account", num);
        Dbuser dbuser = userMapper.selectOne(wrapper);
        if (dbuser!=null){
            return null;
        }else {
            Dbuser dbuser1 = new Dbuser();
            dbuser1.setAccount(num);
            dbuser1.setPassword(pass);
            userMapper.insert(dbuser1);
            return dbuser1;
        }


    }

    @Override
    public Dbuser login(String num, String pass) {
        //判断num为空的情况
        if (!StringUtils.hasText(num)){
            return null;
        }
        //查询是否存在
        QueryWrapper<Dbuser> dbuserQueryWrapper = new QueryWrapper<>();
        dbuserQueryWrapper.eq("account", num).eq("password",pass);
        Dbuser dbuser = userMapper.selectOne(dbuserQueryWrapper);
        return dbuser;
    }

    @Override
    public Dbuser update(String id, String url, String name, String pass) {
        if (!StringUtils.hasText(id)){
            return null;
        }
        if (StringUtils.hasText(url)&&StringUtils.hasText(name)&&StringUtils.hasText(pass)){
            QueryWrapper<Dbuser> dbuserQueryWrapper = new QueryWrapper<>();
            dbuserQueryWrapper.eq("id",id);
            Dbuser dbuser = userMapper.selectById(id);
            dbuser.setImg(url);
            dbuser.setName(name);
            dbuser.setPassword(pass);
            userMapper.updateById(dbuser);
            return dbuser;
        }
        if (StringUtils.hasText(url)&&StringUtils.hasText(name)){
            QueryWrapper<Dbuser> dbuserQueryWrapper = new QueryWrapper<>();
            dbuserQueryWrapper.eq("id",id);
            Dbuser dbuser = userMapper.selectById(id);
            dbuser.setImg(url);
            dbuser.setName(name);
            userMapper.update(dbuser,dbuserQueryWrapper);
            return dbuser;
        }
        if (StringUtils.hasText(url)){
            QueryWrapper<Dbuser> dbuserQueryWrapper = new QueryWrapper<>();
            dbuserQueryWrapper.eq("id",id);
            Dbuser dbuser = userMapper.selectById(id);
            dbuser.setImg(url);
            userMapper.update(dbuser,dbuserQueryWrapper);
            return dbuser;
        }
        if (StringUtils.hasText(name)&&StringUtils.hasText(pass)){
            QueryWrapper<Dbuser> dbuserQueryWrapper = new QueryWrapper<>();
            dbuserQueryWrapper.eq("id",id);
            Dbuser dbuser = userMapper.selectById(id);
            dbuser.setName(name);
            dbuser.setPassword(pass);
            userMapper.update(dbuser,dbuserQueryWrapper);
            return dbuser;
        }
        if (StringUtils.hasText(pass)){
            QueryWrapper<Dbuser> dbuserQueryWrapper = new QueryWrapper<>();
            dbuserQueryWrapper.eq("id",id);
            Dbuser dbuser = userMapper.selectById(id);
            dbuser.setPassword(pass);
            userMapper.update(dbuser,dbuserQueryWrapper);
            return dbuser;
        }
        return null;
    }


}
